package com.tengju.bff.interfaces.covercharge.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.idanchuang.component.base.JsonResult;
import com.idanchuang.component.redis.util.DRedisLocks;
import com.tengju.bff.interfaces.covercharge.constant.BizConstants;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.entity.vo.AccountRevisionApplyReqVo;
import com.tengju.bff.interfaces.covercharge.entity.vo.AdminServiceAccountQueryVo;
import com.tengju.bff.interfaces.covercharge.service.IRoleDesensitService;
import com.tengju.bff.interfaces.covercharge.service.IServiceAccountService;
import com.tengju.bff.interfaces.covercharge.util.validator.ValidaUtil;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.zoi7.component.core.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author fwy
 */
@Api(value = "serviceAccount", tags = "收益账户余额查询")
@RestController
@Slf4j
@RequestMapping("/manage/bill/serviceAccount")
@ManageEntrance
public class AdminServiceAccountController {

    @Autowired
    IServiceAccountService serviceAccountService;

    @Resource
    IRoleDesensitService roleDesensitService;

    @GetMapping
    @ApiOperation(value = "收益账户余额查询", notes = "收益账户余额查询")
    public ApiResponse<IPage<AdminServiceAccountQueryVo>> query(String mobile, String codeId, @RequestParam(defaultValue = "0", name = "pageNo", required = true) Integer pageNo, @RequestParam(defaultValue = "10", name = "limit", required = true) Integer limit,@RequestParam String appCode,@RequestParam Long ssoUserId)  {
        Page<AdminServiceAccountQueryVo> resultList = serviceAccountService.queryPageByIdCode(new Page<>(pageNo, limit), codeId, mobile);
        log.info("[支付后台][收益账户余额查询][查看用户:{}]",ssoUserId);
        return ApiResponse.newSuccess(roleDesensitService.checkDesensit(resultList,appCode,ssoUserId));
    }

    /**
     * 为方便订正数据，故提供此接口
     *
     * @param req 订正请求数据，由订正系统调用
     * @return 返回jsonresult
     */
    @PostMapping("/revision")
    @ApiOperation(value = "订正请求数据", notes = "订正请求数据")
    ApiResponse<?> revision(@RequestBody AccountRevisionApplyReqVo req) {
        {
            //基础校验
            ValidaUtil.baseReqParamCheck(req);
            //调数据修复接口
            return DRedisLocks.runWithLock(BizConstants.UPDATE_REVISION_ACCOUNT + req.getApplyId(), 500, () -> {
                try {
                    serviceAccountService.revisionAccountData(req);
                    return ApiResponse.newSuccess();
                } catch (ServiceException e) {
                    log.info("[数据修复][账户数据修复失败][失败原因:{},申请流水applyId:{}]", e.getMessage(), req.getApplyId());
                    return ApiResponse.newFailure(CoverChargeErrorCode.BUSINESS_ERROR.getCode(), e.getMessage());
                } catch (Exception e) {
                    log.error("[数据修复][账户数据修复失败][失败原因:{},申请流水applyId:{}]", e.getMessage(), req.getApplyId());
                    return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), e.getMessage());
                }
            });
        }
    }

}
